
let util = {

};
util.title = function(title) {
	title = title || window.app.title;
	window.document.title = title;
};

util.inOf = function(arr, targetArr) {
	let res = true;
	arr.forEach(item => {
		if(targetArr.indexOf(item) < 0) {
			res = false;
		}
	});
	return res;
};

util.oneOf = function(ele, targetArr) {
	if(targetArr.indexOf(ele) >= 0) {
		return true;
	} else {
		return false;
	}
};

util.showThisRoute = function(itAccess, currentAccess) {
	if(typeof itAccess === 'object' && Array.isArray(itAccess)) {
		return util.oneOf(currentAccess, itAccess);
	} else {
		return itAccess === currentAccess;
	}
};

util.getRouterObjByName = function(routers, name) {
	if(!name || !routers || !routers.length) {
		return null;
	}
	// debugger;
	let routerObj = null;
	for(let item of routers) {
		if(item.name === name) {
			return item;
		}
		routerObj = util.getRouterObjByName(item.children, name);
		if(routerObj) {
			return routerObj;
		}
	}
	return null;
};

util.handleTitle = function(vm, item) {
	if(typeof item.title === 'object') {
		return vm.$t(item.title.i18n);
	} else {
		return item.title;
	}
};




util.setStore = (name, content) => {
	if(!name) return;
	if(typeof content !== 'string') {
		content = JSON.stringify(content);
	}
	if(process.env.NODE_ENV == 'development') {
		window.localStorage.setItem(name, content);
	} else {
		window.localStorage.setItem(name, content);
	}
}
util.getStore = name => {
	if(!name) return;
	if(process.env.NODE_ENV == 'development') {
		return window.localStorage.getItem(name);
	} else {
		return window.localStorage.getItem(name);
	}
}
util.removeStore = name => {
	if(!name) return;
	if(process.env.NODE_ENV == 'development') {
		window.localStorage.removeItem(name);
	} else {
		window.localStorage.removeItem(name);
	}
}

util.S4 = () => {
	return(((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

util.guid = () => {
	return(util.S4() + util.S4() + "-" + util.S4() + "-" + util.S4() + "-" + util.S4() + "-" + util.S4() + util.S4() + util.S4());
}

util.logout = () => {
	util.removeStore('userId')
	util.removeStore('username')
	util.removeStore('name')
	util.removeStore('email')
	util.removeStore('token')
	util.removeStore('groupid')
	util.removeStore('isfirst')
	window.location.reload()
}

function auto_width(ws, data){
	/*set worksheet max width per col*/
	const colWidth = data.map(row => row.map(val => {
			/*if null/undefined*/
			if (val == null) {
					return {'wch': 10};
			}
			/*if chinese*/
			else if (val.toString().charCodeAt(0) > 255) {
					return {'wch': val.toString().length * 2};
			} else {
					return {'wch': val.toString().length};
			}
	}))
	/*start in the first row*/
	let result = colWidth[0];
	for (let i = 1; i < colWidth.length; i++) {
			for (let j = 0; j < colWidth[i].length; j++) {
					if (result[j]['wch'] < colWidth[i][j]['wch']) {
							result[j]['wch'] = colWidth[i][j]['wch'];
					}
			}
	}
	ws['!cols'] = result;
}
function json_to_array(key, jsonData){
	return jsonData.map(v => key.map(j => { return v[j] }));
}



export default util;